<?php
/**
* 支付宝获取接口
* @author ldward
* @date 2018.10.16
* @copyright leemanpaper.com
*/

require_once ('/data/www/oms/alipay3.3/AopSdk.php');
require_once ('/data/www/oms/custom/lib/pclzip.lib.php');  

try
{
    header("Content-type: text/html; charset=utf-8");
	date_default_timezone_set("PRC");


    $aop = new AopClient ();
    $aop->gatewayUrl = 'https://openapi.alipay.com/gateway.do';
    $aop->appId = '2018082161143173';
    $aop->rsaPrivateKey = 'MIIEowIBAAKCAQEAvzE36e+k01DLaHnU6AwQOWBi6nESP+LC70WOWG9p5+jwFy5ovZUtGeIFDzlWnxl8S0ysylUJhK7rapczVtyHmgIy7IQeEoHruUheV5H06ROgvrPpd9STJNSRafxpja5RdRgth01WUjJIEFLNtViQGjvIoFeCVJvBZnGVJrmDYCxgFLizaXgGUEGO1J049dqeoQo2mjQhVF4VHS6cCoq6k1Iabc0t3NqmWlVKRtXCJ84Q/VPcIZhOirAQij3O7bv3hXJh16wHQ7aHR8AtxLiL3J7eScmeCLjvtmHfomK6eVLVpry6sJ7y+nXDaRI5OnzHpTwiatmDH8oOC6/14yrvDwIDAQABAoIBAAwT4+BXTyY6OF9FMvnlqTC2sgj1U53NiVrsHe3gO+R2hpQwTlWLkunLqV4o1Rv/1CY9lbzCWnOTSTPWkVuvWrGT6fue7PdmVanCtJL7mWkP4Furqdf0CceN1hHH7DMTC/pFqLqfXBCdNIgzcuM90PyXQjk+zfDiVflOu4lStsXga8UkI59g2KtAF/mhXKh5MOGFvlmuqcJpuK3A6l6E6LiHXf6yay1yvhbFW7OrPxH6dD6LfDuQC5e3px9nZ45C2mhCxy981DvkHX9WyUOYY0Al3j/MnVfHjLZMdURO0Tn1yRFsJjgaQkG3w4vOsWmm/i0OSj/HaW9u1RsIiUbayOECgYEA6GNw76U7k2E6LHv0pCtZxAByyP1PjnnbYP5H4T4gJbho6dCA6sW5mVd+ak9ro9fQwYvwXtcNnfx/wz7RU+nTGn5tWIb+m9rXq0K1WLlxQpU01jXdCO2u8SH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8AtxLiL3J7eScmeCLjvtmHfomK6eVLVpry6sJ7y+nXDaRI5OnzHpTwiatmDH8oOC6/14yrvDwIDAQAB"; 

    $aop->apiVersion = '1.0';
    $aop->signType = 'RSA2';
    $aop->postCharset='UTF-8';
    $aop->format='json';
    $bill_date=$_POST['bill_date'];
    $request = new AlipayDataDataserviceBillDownloadurlQueryRequest ();
    $request->setBizContent("{" .
    "\"bill_type\":\"trade\"," .
    "\"bill_date\":\"$bill_date\"" .
    "  }");
    $result = $aop->execute ( $request); 
    $resultbill=json_decode($result,true);
        
    if($resultbill['code']=="10000" && $resultbill['msg']=='Success')
    {
        //http://dwbillcenter.alipay.com/downloadBillFile.resource?bizType=trade&userId=20881213102235930156&fileType=csv.zip&bizDates=201809&downloadFileName=20881213102235930156_201809.csv.zip&fileId=%2Ftrade%2F20881213102235930156%2F201809.csv.zip&timestamp=1540261835&token=232fe69185c411c4896ecf5db98fc307
        //保存文件到本地
        $bill_download_url=$resultbill['bill_download_url'];
        $paramurl=explode("&", $bill_download_url);
        foreach ($paramurl as $key => $value) 
        {
            $tmpfilename=explode("=", $value);
            if($tmpfilename[0]=="downloadFileName")
            {
                $filename=$tmpfilename[1];
                break;
            }
        }
        //创建保存目录
        $tmpdir="/data/www/oms/download/";
        if (!file_exists($tmpdir) && !mkdir($tmpdir, 0777, true)) {
            return false;
        }
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $bill_download_url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
        $content = curl_exec($ch);
        curl_close($ch);
        $size = strlen($content);
        
        $fp2 = @fopen($tmpdir . $filename, 'a');
        fwrite($fp2, $content);
        fclose($fp2);
        unset($content, $bill_download_url);
        
        //解压到指定目录
        $fileurl=explode(".", $filename);
        if (!file_exists($tmpdir.$fileurl[0]."/") && !mkdir($tmpdir.$fileurl[0]."/", 0777, true)) {
            return false;
        }
        $zip = new PclZip($tmpdir.$filename);
        $zip->extract($tmpdir.$fileurl[0]);
        
        //返回指定路径的文件：
        $dir=$tmpdir.$fileurl[0]."/";

        if (is_dir($dir)) 
        {
            if ($dh = opendir($dir)) 
            {
                while (($file = readdir($dh)) !== false) 
                {
                    if(filetype($dir . $file)=="file")
                    {
                        $res[]=array("fileurl"=>$dir.iconv("gbk", "utf-8//ignore", $file),"filename"=>iconv("gbk", "utf-8//ignore", $file));
                    }
                    //echo "filename: ". iconv("gbk", "utf-8//ignore", $file)." : filetype: " . filetype($dir . $file) . "\n";
                } 
                closedir($dh);
            }
        }
        echo json_encode($res);

    }
        

  	}catch (Exception $e) 
    {
        echo $e->getMessage();
    }
